extends Node2D

enum State{
	STATE1,
	STATE2,
	STATE3,
}



@export var max_energy:float =300 #最大能量
@export var add_energy:float = 20  #每次增加的值
@export var reduce_energy:float = 5
@export var energy_state1:float = 100
@export var energy_state2:float = 200
@export var energy_state3:float = 300
@export var change_stop_time:float = 0

var cur_energy:float = 0
var cur_state: int = -1

@onready var interval_timer = $IntervalTimer
@onready var judge_timer = $JudgeTimer
@onready var InitTimer = $InitTimer

func _ready():
	cur_energy = 0
	InitTimer.start()
	

func _physics_process(delta):
	cur_energy = clampf(cur_energy - reduce_energy*delta,0,max_energy)
	if change_stop_time > 0:
		cur_energy = cur_energy + reduce_energy*delta
		change_stop_time = change_stop_time - delta
	if cur_energy < energy_state1:
		if cur_state != State.STATE1:
			cur_state = State.STATE1
			GlobalSignals.emit_signal("energy_state_change",cur_state)
			
	elif cur_energy < energy_state2:
		if cur_state != State.STATE2:
			cur_state = State.STATE2
			GlobalSignals.emit_signal("energy_state_change",cur_state)
			
	else:
		if cur_state != State.STATE3:
			cur_state =State.STATE3
			GlobalSignals.emit_signal("energy_state_change",cur_state)
			
	
#间隔时间
func _on_interval_timer_timeout():
	GlobalSignals.emit_signal("beat_star")
	judge_timer.start()

#手动根据bpm计算每一拍之间的间隔时间，再根据需要的判定时间长度规划两个的timer的间隔
#节拍中
func _on_judge_timer_timeout():
	GlobalSignals.emit_signal("beat_stop")
	interval_timer.start()

func energy_add():
	cur_energy= min(max_energy,cur_energy + add_energy)


func _on_init_timer_timeout():
	GlobalSignals.emit_signal("beat_star")
	interval_timer.start()
